﻿namespace LearnDSQueue;

public class QueuesLinkedList
{
    private Node front;
    private Node rear;
    private int size;

    public QueuesLinkedList()
    {
        front = null;
        rear = null;
        size = 0;
    }

    public int Length()
    {
        return size;
    }

    public bool IsEmpty()
    {
        return size == 0;
    }

    public void Enqueue(int e)
    {
        Node newest = new Node(e, null);
        if (IsEmpty())
        {
            front = newest;
        }
        else
        {
            rear.next = newest;
        }

        rear = newest;
        size++;
    }

    public int Dequeue()
    {
        if (IsEmpty())
        {
            Console.WriteLine("Queue is empty");
            return -1;
        }

        int e = front.element;
        front = front.next;
        size--;
        if (IsEmpty())
            rear = null;
        return e;
    }

    public void Display()
    {
        Node p = front;
        while (p != null)
        {
            Console.WriteLine(p.element + " ");
            p = p.next;
        }

        Console.WriteLine();
    }
}